package com.congee02.modelshellmvn.manager.token;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.congee02.modelshellmvn.entity.account.Account;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
public class AccountJwtSigner {

    @Resource
    private Algorithm algorithm;

    @Value("${jwt.secret.expire.millis}")
    private long expireMillis;

    public void signJwt(Account account) {
        account.setJwt(
                JWT.create()
                        .withSubject(account.getUsername()) // 设置主题为用户名
                        .withExpiresAt(new Date(System.currentTimeMillis() + expireMillis)) // 设置过期时间为当前时间 + 1 小时
                        .withClaim(
                                JwtClaims.ROLE, account.getRole()
                        )
                        .sign(algorithm)
        );
    }

}
